- /* sifismul.cpp by K.Tsuru */
- // function ID = 424 BRADIX
- /*****************************************
- SInteger class
- It multiplies SInteger 'm' by ulong integer 'x'.
- result = m*x for x < ULONG_MAX
- ******************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- void IsMult(const SInteger& m, ulong x, SInteger& result){
- if(!m.Sign(424) || x == 1UL){ result = m; return; }
- if(x == 0){ result.SetZero(); return; }
- const ulong mt = m.SlOpMaxValue();
- if(x > mt){
- SInteger s(x);
- result = NLLMult(m, s);
- return;
- // m.SetError(m.OUT_OF_RANGE,"IsMult", 424);
- }
- uint rh = m.Head(), rt = m.Tail();
- if( &m != &result ){
- //When &m == &result do not execute/not need.
- if(result.Size() < rh+1u) result.valloc(rh+1u, -1);
- //It fills the not substituted part with zero.
- if(rt) result.figure.clear(result.aTail, rt-1u);
- result.figure.clear(rh+1u);
- }
-
- const fType* mv = m.ReadFigures();
- fType* rv = result.figure.Elements();
- #ifndef NDEBUG
- result.figure(rh); m.figure(rh);
- #endif
- register uint i;
- ulong t = 0, rdx1 = (ulong)BRADIX1;
-
- for(i = rt; i <= rh; i++) {
- t += (ulong)mv[i]*x;
- rv[i] = fType(t & rdx1);
- t >>= BRADIX_BITS;
- }
-
- if(t){//carry to the top
- if(t < BRADIX){
- rh++;
- result.Reserve(rh); // result.size() >= rh+i+1
- result.figure[rh] = (fType)t;
- rv = result.figure.Elements();
- } else {//It normalizes the carry and copy to the top.
- fType temp[5];
- i = 0;
- while(t){
- temp[i] = fType(t & rdx1);
- t >>= BRADIX_BITS; i++;
- }
- result.Reserve(rh+i); // result.size() >= rh+i+1
- rv = result.figure.Elements();
- memcpy( (rv+rh+1u), temp, i*sizeof(fType));
- rh += i;
- }
- }
- result.aHead = rh;
- while( !rv[rt] ) rt++;
- result.aTail = rt;
- #ifndef NDEBUG
- result.figure(rh);
- #endif
- result.SetSign(m.RawSign());
- if( 2u*(result.aHead+1) <= result.figure.size() ) result.DoCutDown();
- }
sifismul.cpp : last modifiled at 2017/03/13 14:31:59(1,933 bytes)
created at 2016/04/25 14:53:17
The creation time of this html file is 2017/10/25 11:09:45 (Wed Oct 25 11:09:45 2017).